package com.shadow.bus.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.shadow.bus.config.WorkStatusCode;
import com.shadow.bus.entity.BusInfoEntity;
import com.tepth.shadow.ui.badge.BadgeView;

import java.util.List;

import busfix.tepth.com.shadow_bus.R;

/**
 * Created by 张宇 on 2017/3/15.
 */

public class BusListAdapter extends BaseQuickAdapter<BusInfoEntity, BaseViewHolder> {
    private Drawable mDrawableOnHand;
    private Drawable mDrawableRequestForRepair;
    private Drawable mDrawableMaintenance;
    private Drawable mDrawableComplete;

    public BusListAdapter(Context context, int layoutResId, List<BusInfoEntity> data) {
        super(layoutResId, data);
        mDrawableOnHand = context.getResources().getDrawable(R.drawable.ic_bus_onhand);
        mDrawableRequestForRepair = context.getResources().getDrawable(R.drawable.ic_bus_request_repair);
        mDrawableMaintenance = context.getResources().getDrawable(R.drawable.ic_bus_under_work);
        mDrawableComplete = context.getResources().getDrawable(R.drawable.ic_bus_complete);
    }

    @Override
    protected void convert(BaseViewHolder helper, BusInfoEntity item) {
        helper.addOnClickListener(R.id.layout_parent);
        helper.setText(R.id.tv_number, mContext.getString(R.string.bus_number) + item.getVehicleCode());
        helper.setText(R.id.tv_line, mContext.getString(R.string.bus_line) + item.getRouteName());
        if (item.getLastAlarmTime() == null || "null".equals(item.getLastAlarmTime())
                || item.getLastAlarmTime().isEmpty()) {
            helper.setText(R.id.tv_alarm_time, mContext.getString(R.string.last_alarm_time)
                    + "无");
        } else {
            helper.setText(R.id.tv_alarm_time, mContext.getString(R.string.last_alarm_time)
                    + item.getLastAlarmTime());
        }
        AppCompatImageView imageView = helper.getView(R.id.iv_bus);
        if (WorkStatusCode.UNDER_PLAN.equals(item.getWorkStatusCode())) {
            imageView.setImageDrawable(mDrawableOnHand);
        } else if (WorkStatusCode.FINISH_PLAN.equals(item.getWorkStatusCode())
                || WorkStatusCode.READY_WORK.equals(item.getWorkStatusCode())) {
            imageView.setImageDrawable(mDrawableRequestForRepair);
        } else if (WorkStatusCode.UNDER_WORK.equals(item.getWorkStatusCode())) {
            imageView.setImageDrawable(mDrawableMaintenance);
        } else if (WorkStatusCode.FINISH_WORK.equals(item.getWorkStatusCode())
                || WorkStatusCode.COMPLETE_WORK.equals(item.getWorkStatusCode())) {
            imageView.setImageDrawable(mDrawableComplete);
        }
        //报警
        int level1 = (int) item.getLevel1();
        BadgeView b1 = helper.getView(R.id.badge_3);
        b1.setBadgeCount(level1);
        helper.setVisible(R.id.iv_alarm_3, level1 > 0);

        int level2 = (int) item.getLevel2();
        BadgeView b2 = helper.getView(R.id.badge_2);
        b2.setBadgeCount(level2);
        helper.setVisible(R.id.iv_alarm_2, level2 > 0);

        int level3 = (int) item.getLevel3();
        BadgeView b3 = helper.getView(R.id.badge_1);
        b3.setBadgeCount(level3);
        helper.setVisible(R.id.iv_alarm_1, level3 > 0);
    }

}
